package com.upcode.weighing.vo.resp;


import com.upcode.weighing.vo.resp.pc.AdministrativeAreaRespVo;

import java.util.ArrayList;
import java.util.List;

public class AdministrativeAreaTree {

    private List<AdministrativeAreaRespVo> areaTreeList = new ArrayList<>();

    public AdministrativeAreaTree(List<AdministrativeAreaRespVo> areaTreeList) {
        this.areaTreeList=areaTreeList;
    }

    //建立树形结构
    public List<AdministrativeAreaRespVo> builTree(){
        List<AdministrativeAreaRespVo> treeMenus =new ArrayList<>();
        for(AdministrativeAreaRespVo menuNode : getRootNode()) {
            menuNode = buildChilTree(menuNode);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private AdministrativeAreaRespVo buildChilTree(AdministrativeAreaRespVo pNode){
        List<AdministrativeAreaRespVo> chilMenus =new ArrayList<>();
        for(AdministrativeAreaRespVo menuNode : areaTreeList) {
            if(menuNode.getPid().equals(pNode.getId())) {
                chilMenus.add(buildChilTree(menuNode));
            }
        }
        pNode.setChildren(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<AdministrativeAreaRespVo> getRootNode() {
        List<AdministrativeAreaRespVo> rootMenuLists =new ArrayList<>();
        for(AdministrativeAreaRespVo menuNode : areaTreeList) {
            if(menuNode.getPid().equals("0")) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
